/**
 * Created by lvlq on 16/3/12.
 */
var Q = require("q");

module.exports.index = function (req, res) {
    var userinfo = {
        openid: 'owUQswAmAVkFdFRY_aCtRSiw1Z4g',
        nickname: '吕立奇',
        sex: 1,
        language: 'zh_CN',
        city: 'Dalian',
        province: 'Liaoning',
        country: 'CN',
        headimgurl: 'http://wx.qlogo.cn/mmopen/Q3auHgzwzM59E1IFfoXEsgtXdrCA9E1jN7FBnAAQSIoZ2My3D5uX7sA45BHXgC1JrzqZhry3epS4GTSa40Hseg/0',
        privilege: [],
        type: 'userinfo'
    };


    // req.session = req.session || {};
    req.session.user = userinfo;
    req.session.debug = 1;
    // res.cookie("user", JSON.stringify(userinfo), {maxAge: 2 * 60 * 60 * 1000});
    var next = "/index/home?openid=" + userinfo.openid;
    if (req.query.next) {
        if (req.query.next.split("?openid").length > 1) {
            next = req.query.next + "&oid=" + userinfo.openid;
        } else if (req.query.next.split("?").length > 1) {
            next = req.query.next + "&openid=" + userinfo.openid;
        } else {
            next = req.query.next + "?openid=" + userinfo.openid;
        }
    }


    console.log(next);
    saveUser(userinfo).then(function () {
        res.redirect(next);
    }).catch(function () {
        res.redirect(next);
    })

};

module.exports.indexpost = function (req, res) {
    var userinfo = JSON.parse(new Buffer(req.body.data, 'base64').toString());
    // req.session = req.session || {};
    req.session.user = userinfo;

    // res.cookie("user", JSON.stringify(userinfo), {maxAge: 2 * 60 * 60 * 1000});
    var next = "/index/home?openid=" + userinfo.openid;
    if (req.query.next) {
        if (req.query.next.split("?openid").length > 1) {
            next = req.query.next + "&oid=" + userinfo.openid;
        } else if (req.query.next.split("?").length > 1) {
            next = req.query.next + "&openid=" + userinfo.openid;
        } else {
            next = req.query.next + "?openid=" + userinfo.openid;
        }
    }
    console.log(next);
    saveUser(userinfo).then(function () {
        res.redirect(next);
    }).catch(function () {
        res.redirect(next);
    })
};

var saveUser = function (user) {
    var defer = Q.defer();

    var User = M("index.user");

    User.find({
        where: {
            openid: user.openid
        }
    }).then(function (data) {
        if (data) {
            return User.update({
                openid: user.openid,
                nickname: user.nickname,
                sex: user.sex,
                language: user.language,
                city: user.city,
                country: user.country,
                headimgurl: user.headimgurl
            }, {
                where: {
                    openid: user.openid
                }
            })
        } else {
            return User.create({
                openid: user.openid,
                nickname: user.nickname,
                sex: user.sex,
                language: user.language,
                city: user.city,
                country: user.country,
                headimgurl: user.headimgurl
            });
        }

    }).then(function () {
        defer.resolve();
    }).catch(function (err) {
        console.log(err.message);
        defer.reject(err);
    });

    return defer.promise;
};
